package com.hgyc.mom.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.common.entity.SysDictItem;
import com.hgyc.mom.common.enums.CommonStatus;
import com.hgyc.mom.common.util.DictUtils;
import com.hgyc.mom.system.service.SysDictItemService;
import com.hgyc.mom.system.mapper.SysDictItemMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典项 服务实现类
 * </p>
 *
 * @author fwj
 * @since 2025-01-16
 */
@Slf4j
@Service
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init()
    {
        log.info("加载字典缓存数据！");
        loadingDictCache();
    }

    @Override
    public void add(SysDictItem item) {
        this.save(item);

        //更新缓存
        String dictCode = item.getDictCode();
        List<SysDictItem> itemList = this.getByDictCode(dictCode);
        DictUtils.setDictCache(dictCode, itemList);
    }

    @Override
    public void updateItem(SysDictItem item) {
        this.updateById(item);

        //更新缓存
        String dictCode = item.getDictCode();
        List<SysDictItem> itemList = this.getByDictCode(dictCode);
        DictUtils.setDictCache(dictCode, itemList);
    }

    @Override
    public List<SysDictItem> getItemByCode(String code) {
        List<SysDictItem> itemList = DictUtils.getDictCache(code);
        if (CollectionUtils.isEmpty(itemList)) {
            itemList = this.getByDictCode(code);

            if (!CollectionUtils.isEmpty(itemList)) {
                DictUtils.setDictCache(code, itemList);
            }
        }
        return itemList;
    }

    private List<SysDictItem> getByDictCode(String dictCode) {
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictItem::getDictCode, dictCode);
        return this.list(queryWrapper);
    }

    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingDictCache()
    {
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictItem::getStatus, CommonStatus.ENABLE.getValue());
        List<SysDictItem> itemList = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(itemList)) {
            Map<String, List<SysDictItem>> dictDataMap = itemList.stream().collect(Collectors.groupingBy(SysDictItem::getDictCode));
            for (Map.Entry<String, List<SysDictItem>> entry : dictDataMap.entrySet())
            {
                DictUtils.setDictCache(entry.getKey(), entry.getValue().stream().sorted(Comparator.comparing(SysDictItem::getSort)).collect(Collectors.toList()));
            }
        }
    }

    /**
     * 清空字典缓存数据
     */
    @Override
    public void clearDictCache()
    {
        DictUtils.clearDictCache();
    }

    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache()
    {
        clearDictCache();
        loadingDictCache();
    }
}
